package com.admin.business.user;

import com.admin.pojo.annotation.RepeatSubmit;
import com.admin.utils.RedisUtilKey;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 用户表
 * @create: 2021-08-17 17:19:32
 **/
@Service("user")
@Slf4j
public class UserService{

    @Resource
    UserDao userDao;
    @Resource
    private RedisUtilKey redisUtilKey;

    /**
     * 添加用户表,获取id数据
     *
     * @param cdoRequest - id:自增ID
     *                   - name:用户名
     *                   - phone:手机号
     *                   - state:启用状态：0:未启用，1启用
     *                   - lastActiveTime:最后活跃时间
     *                   - createTime:创建日期
     *                   - updateTime:更新时间
     * @return
     * @author 
     * @create: 2021-08-17 17:19:32
     */
    @OpenApi
    public Return addUser(CDO cdoRequest) {
        userDao.addUser(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，更新用户表数据
     *
     * @param cdoRequest - id:自增ID
     *                   - name:用户名
     *                   - phone:手机号
     *                   - state:启用状态：0:未启用，1启用
     *                   - lastActiveTime:最后活跃时间
     *                   - createTime:创建日期
     *                   - updateTime:更新时间
     * @return
     * @author 
     * @create: 2021-08-17 17:19:32
     */
    @OpenApi
    public Return updateUserById(CDO cdoRequest) {
        int affectedRows = userDao.updateUserById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，删除用户表数据
     *
     * @param cdoRequest - id:自增ID
     * @return
     * @author 
     * @create: 2021-08-17 17:19:32
     */
    @OpenApi
    public Return deleteUserById(CDO cdoRequest){
        int affectedRows = userDao.deleteUserById(cdoRequest.getLong("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除用户表数据
     *
     * @param cdoRequest - phone:手机号
     * @return
     * @author 
     * @create: 2021-08-17 17:19:32
     */
    @OpenApi
    public Return deleteUser(CDO cdoRequest) {
        int affectedRows = userDao.deleteUser(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取用户表数据
     *
     * @param cdoRequest - id:自增ID
     * @return
     * @author 
     * @create: 2021-08-17 17:19:32
     */
    @OpenApi
    public Return getUserById(CDO cdoRequest) {
        CDO cdoMapperResult = userDao.getUserById(cdoRequest.getLong("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取用户表数据
     *
     * @param cdoRequest - id:自增ID
     *                   - phone:手机号
     * @return
     * @author 
     * @create: 2021-08-17 17:19:32
     */
    @OpenApi
    public Return getUser(CDO cdoRequest) {
        CDO cdoMapperResult = userDao.getUser(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取用户表数据列表
     *
     * @param cdoRequest - phone:手机号
     * @return
     * @author 
     * @create: 2021-08-17 17:19:32
     */
    @OpenApi
    public Return getUserList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = userDao.getUserList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取用户表数据列表
     *
     * @param cdoRequest - id:自增ID
     *                   - phone:手机号
     * @return
     * @author 
     * @create: 2021-08-17 17:19:32
     */
    @OpenApi
    public Return getUserListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = userDao.getUserListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
        return Return.valueOf(cdoResult);
    }
}
